package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.WxGoodsListBO;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.WxGoodsService;
import com.cskaoyan.util.CurrentUesr;
import com.cskaoyan.util.MyPageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Fengyi
 * @description:
 * @time: 2021/7/12 10:20
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {

    @Autowired
    MallGoodsMapper mallGoodsMapper;
    @Autowired
    MallBrandMapper mallBrandMapper;
    @Autowired
    MallCommentMapper mallCommentMapper;
    @Autowired
    MallGoodsSpecificationMapper mallGoodsSpecificationMapper;
    @Autowired
    MallGoodsAttributeMapper mallGoodsAttributeMapper;
    @Autowired
    MallIssueMapper mallIssueMapper;
    @Autowired
    MallGoodsProductMapper mallGoodsProductMapper;
    @Autowired
    MallCategoryMapper mallCategoryMapper;
    @Autowired
    MallCollectMapper mallCollectMapper;
    @Autowired
    MallFootprintMapper mallFootprintMapper;
    @Autowired
    MallSearchHistoryMapper mallSearchHistoryMapper;

    @Override
    public int queryGoodsCount() {
        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria mallGoodsExampleCriteria = mallGoodsExample.createCriteria();
        mallGoodsExampleCriteria.andDeletedEqualTo(false);
        mallGoodsExampleCriteria.andIsOnSaleEqualTo(true);
        long l = mallGoodsMapper.countByExample(mallGoodsExample);
        return (int) l;
    }

    @Override
    public List<MallGoods> queryGoodsList(WxGoodsListBO wxGoodsListVO) {

        MyPageHelper.startPage(wxGoodsListVO);

        MallCategoryExample mallCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria categoryExampleCriteria = mallCategoryExample.createCriteria();
        categoryExampleCriteria.andDeletedEqualTo(0);


        MallGoodsExample mallGoodsExample = new MallGoodsExample();

        MallGoodsExample.Criteria mallGoodsExampleCriteria = mallGoodsExample.createCriteria();
        mallGoodsExampleCriteria.andDeletedEqualTo(false);
        mallGoodsExampleCriteria.andIsOnSaleEqualTo(true);
        if (wxGoodsListVO.getOrder() != null && wxGoodsListVO.getSort() != null) {
            mallGoodsExample.setOrderByClause("`" + wxGoodsListVO.getSort() + "` " + wxGoodsListVO.getOrder());
        }

        if (wxGoodsListVO.getIsNew() != null) {
            mallGoodsExampleCriteria.andIsNewEqualTo(wxGoodsListVO.getIsNew());
        }

        if (wxGoodsListVO.getIsHot() != null) {
            mallGoodsExampleCriteria.andIsNewEqualTo(wxGoodsListVO.getIsHot());
        }

        if (wxGoodsListVO.getBrandId() != null) {
            mallGoodsExampleCriteria.andBrandIdEqualTo(wxGoodsListVO.getBrandId());
        }

        if (wxGoodsListVO.getCategoryId() != 0) {
            MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(wxGoodsListVO.getCategoryId());

            if (mallCategory.getPid() == 0) {
                List<Integer> childId = mallCategoryMapper.selectChildBypid(mallCategory.getId());
                childId.add(mallCategory.getId());
                mallGoodsExampleCriteria.andCategoryIdIn(childId);
            } else {
                mallGoodsExampleCriteria.andCategoryIdEqualTo(wxGoodsListVO.getCategoryId());
            }


        }
        if (wxGoodsListVO.getKeyword() != null) {
            mallGoodsExampleCriteria.andKeywordsLike("%" + wxGoodsListVO.getKeyword() + "%");
            MallSearchHistory record = new MallSearchHistory();

            record.setUserId(CurrentUesr.getUserId());
            record.setKeyword(wxGoodsListVO.getKeyword());
            record.setAddTime(new Date());
            mallSearchHistoryMapper.insertSelective(record);
        }

        List<MallGoods> mallGoodsList = mallGoodsMapper.selectByExample(mallGoodsExample);

        return mallGoodsList;
    }

    @Override
    public WxGoodsDetailVO queryGoodsDetail(Integer id) {
        WxGoodsDetailVO wxGoodsDetailVO = new WxGoodsDetailVO();

        //todo username
        Subject subject = SecurityUtils.getSubject();

        wxGoodsDetailVO.setUserHasCollect(0);
        if(subject.getPrincipals() != null) {
            int uid = CurrentUesr.getUserId();
            int ifCollect = mallCollectMapper.ifCollect(uid, id);
            wxGoodsDetailVO.setUserHasCollect(ifCollect);
        }

        //
        wxGoodsDetailVO.setShareImage("");

        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria mallGoodsExampleCriteria = mallGoodsExample.createCriteria();
        mallGoodsExampleCriteria.andDeletedEqualTo(false);

        //setInfo
        MallGoods mallGoods = mallGoodsMapper.selectByPrimaryKey(id);
        wxGoodsDetailVO.setInfo(mallGoods);

        //setBrand
        MallBrand mallBrand = mallBrandMapper.selectByPrimaryKey(mallGoods.getBrandId());
        wxGoodsDetailVO.setBrand(mallBrand);

        //setComment
        MallCommentExample mallCommentExample = new MallCommentExample();
        MallCommentExample.Criteria commentExampleCriteria = mallCommentExample.createCriteria();
        commentExampleCriteria.andDeletedEqualTo(false);
        commentExampleCriteria.andValueIdEqualTo(id);
        mallCommentExample.setOrderByClause(" id desc limit 4");


        List<MallComment> mallComments = new ArrayList<>();
        mallComments = mallCommentMapper.selectByExample(mallCommentExample);

        WxCommentListVO comment = new WxCommentListVO();
        comment.setData(mallComments);
        comment.setCount((int) mallCommentMapper.countByExample(mallCommentExample));
        wxGoodsDetailVO.setComment(comment);


        //setSpecificationList
        List<WxSpecificationListVO> wxSpecificationListVOS = new ArrayList<>();

        List<String> specs = mallGoodsSpecificationMapper.selectSpecNameByGid(id);
        for (String spec : specs) {
            MallGoodsSpecificationExample mallGoodsSpecificationExample = new MallGoodsSpecificationExample();
            MallGoodsSpecificationExample.Criteria specificationExampleCriteria = mallGoodsSpecificationExample.createCriteria();
            specificationExampleCriteria.andDeletedEqualTo(false);
            specificationExampleCriteria.andGoodsIdEqualTo(id);
            specificationExampleCriteria.andSpecificationEqualTo(spec);

            WxSpecificationListVO wxSpecificationListVO = new WxSpecificationListVO();
            List<MallGoodsSpecification> specifications = mallGoodsSpecificationMapper.selectByExample(mallGoodsSpecificationExample);

            wxSpecificationListVO.setName(spec);
            wxSpecificationListVO.setValueList(specifications);

            wxSpecificationListVOS.add(wxSpecificationListVO);
        }

        wxGoodsDetailVO.setSpecificationList(wxSpecificationListVOS);

        //setAttribute
        MallGoodsAttributeExample mallGoodsAttributeExample = new MallGoodsAttributeExample();
        MallGoodsAttributeExample.Criteria attributeExampleCriteria = mallGoodsAttributeExample.createCriteria();
        attributeExampleCriteria.andDeletedEqualTo(false);
        attributeExampleCriteria.andGoodsIdEqualTo(id);

        List<MallGoodsAttribute> mallGoodsAttributes = mallGoodsAttributeMapper.selectByExample(mallGoodsAttributeExample);
        wxGoodsDetailVO.setAttribute(mallGoodsAttributes);


        //setIssue
        MallIssueExample mallIssueExample = new MallIssueExample();
        MallIssueExample.Criteria issueExampleCriteria = mallIssueExample.createCriteria();
        issueExampleCriteria.andDeletedEqualTo(false);

        List<MallIssue> mallIssues = mallIssueMapper.selectByExample(mallIssueExample);
        wxGoodsDetailVO.setIssue(mallIssues);


        //setProductList
        MallGoodsProductExample mallGoodsProductExample = new MallGoodsProductExample();
        MallGoodsProductExample.Criteria goodsProductExampleCriteria = mallGoodsProductExample.createCriteria();
        goodsProductExampleCriteria.andDeletedEqualTo(false);
        goodsProductExampleCriteria.andGoodsIdEqualTo(id);
        List<MallGoodsProduct> mallGoodsProducts = mallGoodsProductMapper.selectByExample(mallGoodsProductExample);
        wxGoodsDetailVO.setProductList(mallGoodsProducts);

        return wxGoodsDetailVO;
    }

    @Override
    public WxGoodsListVO queryRelatedGoodsList(Integer id) {

        WxGoodsListVO wxGoodsListVO = new WxGoodsListVO();

        int cid = mallGoodsMapper.selectPidById(id);

        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria mallGoodsExampleCriteria = mallGoodsExample.createCriteria();
        mallGoodsExampleCriteria.andDeletedEqualTo(false);
        mallGoodsExampleCriteria.andCategoryIdEqualTo(cid);
        mallGoodsExampleCriteria.andIdNotEqualTo(id);


        mallGoodsExample.setOrderByClause("id desc limit 6");
        List<MallGoods> mallGoodsList = mallGoodsMapper.selectByExample(mallGoodsExample);

        wxGoodsListVO.setGoodsList(mallGoodsList);

        return wxGoodsListVO;
    }


    @Override
    public int insertFootprint(Integer id, int userId) {
        Date addTime = new Date();

        MallFootprintExample mallFootprintExample = new MallFootprintExample();
        MallFootprintExample.Criteria criteria = mallFootprintExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);

        List<MallFootprint> mallFootprints = mallFootprintMapper.selectByExample(mallFootprintExample);
        if (mallFootprints.size() != 0) {
            //说明商品已经记录在足迹中，这样只要更新addTime和updateTime就行
            MallFootprint mallFootprint = new MallFootprint();
            mallFootprint.setAddTime(addTime);
            mallFootprint.setUpdateTime(addTime);

            int update = mallFootprintMapper.updateByExampleSelective(mallFootprint, mallFootprintExample);

            if (update == 1)
                return 200;
            else
                return 500;
        }


        MallFootprint mallFootprint = new MallFootprint(
                null,
                userId,
                id,
                addTime,
                addTime,
                false
        );


        int insert = mallFootprintMapper.insert(mallFootprint);

        if (insert == 1) {
            return 200;
        } else {
            return 500;
        }
    }

}
